<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="660px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form
      :inline="true"
      :model="indModel"
      :rules="formRules"
      ref="indexForm"
      label-width="80px"
      label-position="right"
      size="small"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="序号" prop="indexId">
            <el-input
              :maxlength="50"
              v-model="indModel.indexId"
              autofocus
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指标名称" prop="indexName">
            <el-input :maxlength="50" v-model="indModel.indexName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="正常参数" prop="parameterValues">
            <el-input
              :maxlength="50"
              v-model="indModel.parameterValues"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="company">
            <el-input :maxlength="50" v-model="indModel.company"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="左临界值" prop="leftCritical">
            <el-input
              :maxlength="50"
              v-model="indModel.leftCritical"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="右临界值" prop="rightCritical">
            <el-input
              :maxlength="50"
              v-model="indModel.rightCritical"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="针对性别">
            <el-select v-model="indModel.limits" placeholder="请选择">
              <el-option
                v-for="item in modeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="说明" prop="explains">
            <el-input
              class="properties-textarea"
              type="textarea"
              :maxlength="50"
              v-model="indModel.explains"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否禁用" prop="indexStart">
            <el-switch
              v-model="indModel.indexStart"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="2"
              :inactive-value="1"
            >
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('indexForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { addIndex, upIndex } from "@/api/HealthIntervention/indexManagement";

export default {
  name: "index-edit",
  data() {
    return {
      indModel: {
        indexId: "",
        indexName: "",
        parameterValues: "",
        company: "",
        leftCritical: "",
        rightCritical: "",
        limits: "",
        explains: "",
        indexStart: "1",
      },
      //   下拉框
      modeList: [
        { value: "不限", label: "不限" },
        { value: "男", label: "男" },
        { value: "女", label: "女" },
      ],
      dialogProps: {
        visible: false,
        action: "",
        title: "",
      },
      //输入提示
      formRules: {
        // diaryPerson: [
        //   { required: true, message: '请输入名称', trigger: 'blur' },
        //   { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
        // ],
        // fileNumber: [
        //   { required: true, message: '请输入档案号', trigger: 'blur' },
        //   { min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur' }
        // ],
      },
    };
  },
  methods: {
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs["indexForm"].clearValidate();
      });
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveDiary();
          } else {
            this.doUpdateDiary();
          }
        } else {
          return false;
        }
      });
    },
    handleDialogClose() {
      this.dialogProps.visible = false;
    },
    //编辑
    doUpdateDiary() {
      this.loading = true;
      upIndex(this.indModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            message: "修改成功",
            type: "success",
          });
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    //新增
    doSaveDiary() {
      this.loading = true;
      addIndex(this.indModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
          this.$message({
            message: "新增成功",
            type: "success",
          });
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  mounted: function () {
    this.$nextTick(() => {
      this.$on("openEditIndexDialog", function (router) {
        this.indModel = router;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddIndexDialog", function () {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.indModel = {
          indexStart: 1,
        };
        this.dialogProps.visible = true;
      });
    });
  },
};
</script>

<style lang="scss" scoped>
.properties-textarea {
  width: 496px;
}
</style>





